package cn.pxy.ysframework.web.Controller;

import cn.pxy.ysframework.utils.Message;
import cn.pxy.ysframework.web.Dto.CreateCommentDto;
import cn.pxy.ysframework.web.Entity.Card;
import cn.pxy.ysframework.web.Entity.Comment;
import cn.pxy.ysframework.web.Entity.Favorite;
import cn.pxy.ysframework.web.Service.ICardService;
import cn.pxy.ysframework.web.Service.IFavoriteService;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/Favorite")
@Api(description = "喜爱控制器--完成")
public class FavoriteController {
    @Autowired
    private IFavoriteService favoriteService;

    @Autowired
    private ICardService cardService;

    /**
     * 根据用户id返回所有喜爱的帖子
     * @param accountid
     * @return
     */
    @PostMapping(value = "/FindFavoriteCard")
    @ApiOperation(value = "返回用户最爱的帖子--完成")
    public Message FindFavortieCard(String accountid){
        List<Favorite> Flist = favoriteService.findAllByAccountid(accountid);
        List<Card> CList = new ArrayList<>();
        for (Favorite f:Flist) {
            CList.add(cardService.findById(f.getCardid()));
        }
        return new Message(JSON.toJSONString(CList));
    }

    /**
     * 创建喜爱
     * @param accountid
     * @param cardid
     * @return
     */
    @PostMapping(value = "/CreateFavorite")
    @ApiOperation(value = "关注--完成")
    public Message CreateFavorite(String accountid,String cardid){
        return new Message(JSON.toJSONString(favoriteService.CreateFavorite(accountid,cardid)));
    }
}
